﻿/*******************************************************************************
* Copyright (C) JuCheap
* 
* Author: JuCheap
* Create Date: 2019/11/21 15:50:39
* Description: <Description>
* 
* Revision History:
* Date         Author               Description
*
*********************************************************************************/

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace JuCheap.Core.Infrastructure.Web
{
    /// <summary>
    /// web api统一数据返回格式处理
    /// </summary>
    public class WebApiResponseDataMiddleware : ActionFilterAttribute
    {
        /// <summary>
        /// OnResultExecuting
        /// </summary>
        /// <param name="context"></param>
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.Result is ObjectResult)
            {
                var objectResult = context.Result as ObjectResult;
                context.Result = new ObjectResult(ToResponse(objectResult.StatusCode ?? StatusCodes.Status200OK, objectResult.Value));
            }
            else if (context.Result is EmptyResult)
            {
                context.Result = new ObjectResult(ToNotFound());
            }
            else if (context.Result is ContentResult)
            {
                //原样返回，不做处理
            }
            else if (context.Result is StatusCodeResult)
            {
                var result = context.Result as StatusCodeResult;
                context.Result = new ObjectResult(ToResponse(result.StatusCode, string.Empty));
            }
            else
            {
                base.OnResultExecuting(context);
            }
        }

        /// <summary>
        /// 未找到
        /// </summary>
        /// <returns></returns>
        private ResponseJsonData<object> ToNotFound()
        {
            var responseData = new ResponseJsonData<object>();
            responseData.Status = StatusCodes.Status404NotFound; ;
            responseData.Message = "未找到资源";
            return responseData;
        }

        /// <summary>
        /// 返回指定格式的数据
        /// </summary>
        /// <param name="code"></param>
        /// <param name="value"></param>
        /// <param name="message">提示消息</param>
        /// <returns></returns>
        private ResponseJsonData<object> ToResponse(int code, object value, string message = "")
        {
            message = 415 == code ? "不支持该请求方式" : message;
            var responseData = new ResponseJsonData<object>();
            responseData.Status = code;
            responseData.Result = value;
            responseData.Message = message;
            return responseData;
        }
    }
}
